﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Temp_20190707_AForm
{
    public partial class DockTipPanel : UserControl
    {
        [Obsolete("必须初始化 DockPanel 对象", true)]
        public DockTipPanel()
        {
            InitializeComponent();
        }
        public DockTipPanel(DockPanel dockPanel)
        {
            InitializeComponent();
            this.DockPanel = dockPanel;
            m_Timer = new Timer();
            m_Timer.Interval = 100;
            m_Timer.Tick+=new EventHandler(Timer_Tick);
            m_Timer.Enabled = true;
        }


        private Timer m_Timer = new Timer();
        public DockPanel DockPanel { get; set; }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.Refresh();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            var g = e.Graphics;
            var dockPanel = this.DockPanel;
            if (dockPanel == null || !dockPanel.IsHandleCreated || dockPanel.Disposing || dockPanel.IsDisposed) return;

            var layoutInfo = dockPanel.GetLayoutInfo();
            var tabGroups = layoutInfo.GetTabGroups();

            if (tabGroups != null)
                foreach (var group in tabGroups)
                {
                    DrawDockTabGroup(g, group);
                }
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            this.Refresh();
        }


        public DockEventArgs NewDockEventArgs()
        {
            Point point1 = Control.MousePosition;
            Point point2 = DockPanel.PointToClient(point1);

            var layoutInfo = DockPanel.GetLayoutInfo();
            var tabGroups = layoutInfo.GetTabGroups();

            foreach (var tabGroup in tabGroups)
            {
                var alignment = tabGroup.TipRect.Contains(point2);
                if (alignment != DockAlignment.None)
                {
                    //MessageBox.Show("tabGroup");
                    return new DockEventArgs { Control = tabGroup.TabGroup, TipLayout = tabGroup, Alignment = alignment };
                }
            }


            return null;
        }


        //public void DrawDockContent(Graphics g, DockLayoutInfo layout)
        //{
        //    Color color = Color.FromArgb(200, Color.Teal);
        //    if (layout.MouseInTipRect(DockPanel)) color = Color.FromArgb(200, Color.Thistle);
        //    g.FillRectangle(new SolidBrush(color), layout.TipRect);
        //}
        //public void DrawDockTabGroup(Graphics g, DockLayoutInfo layout)
        //{
        //    Color color = Color.FromArgb(200, Color.Tan);
        //    if (layout.MouseInTipRect(DockPanel)) color = Color.FromArgb(200, Color.Tomato);
        //    g.FillRectangle(new SolidBrush(color), layout.TipRect);
        //}

        public void DrawDockTabGroup(Graphics g, DockLayoutInfo layout)
        {
            Color colorC = Color.FromArgb(200, Color.Coral);
            Color colorT = Color.FromArgb(200, Color.IndianRed);
            Color colorB = Color.FromArgb(200, Color.IndianRed);
            Color colorL = Color.FromArgb(200, Color.CornflowerBlue);
            Color colorR = Color.FromArgb(200, Color.CornflowerBlue);

            var alignment = layout.MouseInTipRect(DockPanel);
            if (alignment == DockAlignment.Center) colorC = Color.FromArgb(255, colorC);
            if (alignment == DockAlignment.Top) colorT = Color.FromArgb(255, colorT);
            if (alignment == DockAlignment.Bottom) colorB = Color.FromArgb(255, colorB);
            if (alignment == DockAlignment.Left) colorL = Color.FromArgb(255, colorL);
            if (alignment == DockAlignment.Right) colorR = Color.FromArgb(255, colorR);

            g.FillRectangle(new SolidBrush(colorC), layout.TipRect.RectCenter);
            g.FillRectangle(new SolidBrush(colorT), layout.TipRect.RectTop);
            g.FillRectangle(new SolidBrush(colorB), layout.TipRect.RectBottom);
            g.FillRectangle(new SolidBrush(colorL), layout.TipRect.RectLeft);
            g.FillRectangle(new SolidBrush(colorR), layout.TipRect.RectRight);
        }
    }
}
